####分析远程图片
# 调用所需模块
import requests
import matplotlib.pyplot as plt
import json
from PIL import Image
from io import BytesIO
# import requests

%matplotlib inline
import matplotlib.pyplot as plt
import json
from PIL import Image


#记住是新建一个计算机视觉资源，而非人脸的那个！
endpoint = "https://cjs.cognitiveservices.azure.cn/"# 将密钥和终结点添加到环境变量中

subscription_key = "c0967f238d8f4639a62b408b6f55214f"# 输入密钥

analyze_url = endpoint + "vision/v3.1/analyze"   #https://cvhzx.cognitiveservices.azure.com/vision/v3.1/analyze

# 放置图片url
image_url = "https://ss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=2648083114,791155285&fm=26&gp=0.jpg" 
# 请求头部信息
headers = {'Ocp-Apim-Subscription-Key': subscription_key}
# 请求参数
params = {'visualFeatures': 'Categories,Description,Color'}
# 请求主题Body
data = {'url': image_url}
response = requests.post(analyze_url, headers=headers,
                         params=params, json=data)
response.raise_for_status()

analysis = response.json()
print(json.dumps(response.json()))
image_caption = analysis["description"]["captions"][0]["text"].capitalize()

image = Image.open(BytesIO(requests.get(image_url).content))
plt.imshow(image)
plt.axis("off")
_ = plt.title(image_caption, size="x-large", y=-0.1)
plt.show()